/**
 * Created by teddy on 16-1-20.
 */


var lineOptions = {
    chart: {
        renderTo: 'lineChart',
        defaultSeriesType: 'line',
    },
    colors: ['#50B432','#ED561B'],
    title: {
        text: 'Fortnight Site Map Smoke Test Chart',
        x: -20
    },
    subtitle: {
        text: '',
        x: -20
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
            month: '%e. %b',
            year: '%b'
        },
        title: {
            text: ''
        }
    },
    yAxis: {
        title: {
            text: 'Count'
        },
        plotLines: [{
            value: 0,
            width: 1,
            color: '#808080'
        }]
    },
    /*Display all the series on one tooltip*/
    tooltip: {
        shared: true,
        crosshairs: true
    },
    credits: {
        enabled: false
    },
    series: [
        {name: 'Success'},
        {name:'Fail'}
    ]
}


$(function () {
    /*User -- HardCode for 21tester*/
    if ($('#usr').val() == "21tester") {
        var username = $('#usr').val();
        $('#loginForm').hide();
        var helloForm = "<span id='helloForm'><span class='btn btn-danger navbar-right' onclick='Logout()' style='color:white;margin-top: 8px'>LogOut</span><span class='navbar-right' style='color:white;margin-right: 15px;font-size:20px;margin-top: 11px'>Hello, " + username + " </span></span>";
        $('#navbar').append(helloForm);
    }

    /*SiteMap Table double click event*/
    $('#SiteMapResultTable').on('dbl-click-row.bs.table', function (e, row, $element) {
        if (row['status'] == 'Success') {
        }
        else {
            $.ajax({
                type: "Get",
                url: "getSiteMapErrorList.php?date=" + row["day"],
                success: function(data) {
                    var errorList = $.parseJSON(data);
                    var tempHTML = '<table data-toggle="table"><thead><tr style="text-align: center"><th>Link</th><th>Return Code</th></tr></thead><tbody>';
                    for (var key in errorList)
                    {
                        tempHTML += '<tr><td><a href="' + errorList[key].sitepath + '" target="_blank">' + errorList[key].sitepath + '</a></td><td style="text-align: center">' + errorList[key].code + '</td></tr>';
                    }
                    tempHTML += '</tbody></table>'
                    swal({
                        title: "Failed Links",
                        text: tempHTML,
                        html: true
                    });
                }
            })
        }
    })

    /*Init the chart*/
    $.ajax({
        type:"GET",
        url:'getLineChartSiteMap.php',
        success: function(tsv){
            var successData = [];
            var failData = [];
            var successCount = 0;
            var failCount = 0;
            try {
                // split the data return into lines and parse them
                tsv = tsv.split(/\n/g);

                jQuery.each(tsv, function(i, line) {

                    if(line[1] != null) {
                        line = line.split(/\t/);
                        var date = Date.parse(line[0] + ' UTC');
                        if(line[1] == "Success"){
                            successData.push([date, parseInt(line[2].replace(',', ''), 10)]);
                            successCount = successCount + parseInt(line[2]);
                        }
                        else {
                            failData.push([date, parseInt(line[2].replace(',', ''), 10)]);
                            failCount = failCount + parseInt(line[2]);
                        }
                    }
                });
            } catch (e) {  }
            lineOptions.series[0].data = successData;
            lineOptions.series[1].data = failData;
            var lineChart = new Highcharts.Chart(lineOptions);
        }
    });
})

/*Row Styles For Job Table*/
function rowStyle(row, index) {
    /*define five type*/
    var classes = ['active', 'success', 'info', 'warning', 'danger'];

    /*if has error*/
    if (row["status"] == "Success") {
        return {
            classes: classes[1]
        };
    }
    /*if no error*/
    else if (row["status"] != "Success")
    {
        return {
            classes: classes[4]
        };
    }
    /*for old data --- no fail column*/
    else if (row["failCount"] == "" || row["failCount"] == null)
    {
        return{
            classes: classes[3]
        }
    }
    /*default*/
    return {};
}